import { defineStore } from 'pinia';
import storage from '@/utils/storage';

/** Global state */
type GlobalState = {
  /**  Loading overlay */
  _loading: boolean;
  /** ProgressBar Percentage */
  _progress: number | null;
  /** SnackBar Text */
  _message: string | null;
  //  菜单是否折叠
  _showMobileMenu: boolean;
  // 默认的tab
  _tab: string;
};

/** Global Store */
export default defineStore('global', {
  // Default Global State
  state: (): GlobalState => ({
    _loading: false,
    _progress: null,
    _message: null,
    _showMobileMenu: false,
    _tab: storage.getItem('tab') ? storage.getItem('tab') : 'Home',
  }),
  // Getters
  getters: {
    /** Loading overlay visibility */
    loading (s): boolean {
      return s._loading;
    },
    /** Loadig progress value */
    progress (s): number | null {
      s._loading = true;
      return s._progress;
    },
    /** Snackbar Text (Unused) */
    message (s): string | null {
      return s._message;
    },
    showMobileMenu (s): boolean {
      return s._showMobileMenu;
    },
    tab (s): string {
      return s._tab;
    },
  },
  // Actions
  actions: {
    /** Show loading Overlay */
    setLoading (display: boolean) {
      this._loading = display;
    },
    /** Update progress value */
    setProgress (progress: number | null) {
      // update progress value
      this._progress = progress;
      // display loading overlay
      this._loading = true;
    },
    /** Show snackbar message */
    setMessage (message: string) {
      // put snackbar text
      this._message = message;
    },
    // 设置菜单是否折叠
    setMenuFoldStatus (fold: boolean) {
      this._showMobileMenu = fold;
    },
    // 设置tab
    setTab (tab: string) {
      this._tab = tab;
    },
  },
  persist: {
    enabled: true,
    // strategies: [
    //   {
    //     key: 'global',
    //     storage: window.localStorage
    //   }
    // ]
  },
});
